unit unOutcomes;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls;

type

  { TfrmOutcomes }

  TfrmOutcomes = class(TForm)
    btnLoad: TButton;
    btnSave: TButton;
    dlgOpen: TOpenDialog;
    dlgSave: TSaveDialog;
    textMust: TStaticText;
    textShould: TStaticText;
    textCould: TStaticText;
    txtCould: TStaticText;
    txtShould: TStaticText;
    txtMust: TStaticText;
    procedure btnLoadClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure pnlMustDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure textCouldClick(Sender: TObject);
    procedure textMustClick(Sender: TObject);
    procedure textShouldClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    Filename : String;
    Group       : String;
    procedure CreateLabels;
    procedure ClearLabels(NewGroup : String);
  end; 


implementation

{ TfrmOutcomes }
Uses
   DataModule;


procedure TfrmOutcomes.ClearLabels(NewGroup : String);
   var i, comps : integer;
   begin
        if NewGroup <> Group then
        begin
             Group := NewGroup;
             i:=0;
             try
                Comps := Self.ComponentCount;
                while i < Comps do
                begin
                     if Self.ComponentCount > i then
                     begin
                          if (Self.Components[i] is TLabel) then
                          begin
                               Self.Components[i].Destroy;
                               Dec(Comps);
                          end
                          else
                              inc(i);
                     end;
                 end;
            Finally
              Self.Refresh;
            end;
        end;
   end;

procedure TfrmOutcomes.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TLabel);
  if accept then
  begin
       TLabel(source).Left:=x;
       TLabel(source).Top :=y;
  end;
end;

procedure TfrmOutcomes.btnLoadClick(Sender: TObject);
  var
      fileline : TStringList;
      FileHandle : TextFile;
      NewLabel   : TLabel;
      Buffer     : String;
  begin
       if dlgOpen.Execute then
       begin
          ClearLabels('');
          AssignFile(FileHandle, dlgOpen.FileName);
          Reset(FileHandle);
          Readln(FileHandle, Group);
          Readln(FileHandle,Buffer);
          TextMust.Caption:=Buffer;
          ReadLn(FileHandle,Buffer);
          TextShould.Caption := Buffer;
          ReadLn(FileHandle,Buffer);
          TextCould.Caption:= Buffer;
          Fileline := TStringList.Create;
          Fileline.Delimiter:=',';
          while not eof(FileHandle) do
          begin
               Readln(FileHandle,Buffer);
               FileLine.DelimitedText:=Buffer;
               NewLabel := TLabel.Create(self);
               NewLabel.DragMode  := dmAutomatic;
               NewLabel.DragCursor:= crHandPoint;
               NewLabel.Parent    := Self;
               NewLabel.Font.Size := 18;
               NewLabel.Caption   := FileLine[0];
               NewLabel.Top       := StrToInt(FileLine[1]);
               NewLabel.Left      := StrToInt(FileLine[2]);
          end;
          FileLine.Destroy;
          CloseFile(FileHandle);
       end;

  end;

procedure TfrmOutcomes.btnSaveClick(Sender: TObject);
var i : Integer;
      fileline : TStringList;
      FileHandle : TextFile;
  begin
     dlgSave.FileName:='Outcomes'+datamodule1.Group;
     if dlgSave.Execute then
     begin
          AssignFile(FileHandle, dlgsave.FileName);
          Rewrite(FileHandle);

          Fileline := TStringList.Create;
          Fileline.Delimiter:=',';
          writeln(FileHandle,Group);
          writeln(FileHandle,TextMust.Caption);
          writeln(FileHandle,TextShould.Caption);
          writeln(FileHandle,TextCould.Caption);
          for i:= 1 to Self.ComponentCount - 1 do
          begin
               if (Self.Components[i] is TLabel) then
               begin
                    fileline.Add(TLabel(Self.Components[i]).Caption);
                    fileline.Add(IntToStr(TLabel(Self.Components[i]).Top));
                    fileline.Add(IntToStr(TLabel(Self.Components[i]).Left));
                    //save to file
                    writeln(FileHandle,fileline.DelimitedText);
                    //clear
                    fileline.Clear;
               end;
          end;
          FileLine.Destroy;
          CloseFile(FileHandle);
     end;
  end;


procedure TfrmOutcomes.pnlMustDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source is TLabel);
  if accept then
  begin
       TLabel(source).Left:=x + (TPanel(Sender).Left);
       TLabel(source).Top :=y + (TPanel(Sender).Top);
  end;
end;

procedure TfrmOutcomes.textCouldClick(Sender: TObject);
begin
    textCould.Caption := InputBox('Lesson Tools', 'Please enter the could', 'COULD:');
end;

procedure TfrmOutcomes.textMustClick(Sender: TObject);
begin
  textMust.Caption := InputBox('Lesson Tools', 'Please enter the must', 'MUST:');
end;

procedure TfrmOutcomes.textShouldClick(Sender: TObject);
begin
  textShould.Caption := InputBox('Lesson Tools', 'Please enter the should', 'SHOULD:');
end;


procedure TfrmOutcomes.CreateLabels;
var
   NewLabel   : TLabel;
   i,col,down : Integer;
   size       : Integer;
begin
    size := 0;
    col  := 0;
    down := 0;
    for i := 0 to DataModule1.FirstNames.Count-1 do
    begin
        NewLabel := TLabel.Create(self);
        NewLabel.DragMode  := dmAutomatic;
        NewLabel.DragCursor:= crHandPoint;
        NewLabel.Parent    := Self;
        NewLabel.Top       := 390 + (Down * 30);
        NewLabel.Left      := size + (col*100);
        NewLabel.Font.Size := 18;
        NewLabel.Caption   := DataModule1.FirstNames.Strings[i];
        size               := Length(NewLabel.Caption) * 3;
        NewLabel.tag       := 9;
        NewLabel.Show;
        inc(col);
        if col > 7 then
        begin
             col  := 0;
             inc(down);
             size := 0;
        end;
    end;
end;



initialization
  {$I unoutcomes.lrs}

end.

